package com.bing.controller.Question;

import com.bing.dto.enumutil.QuestionType;
import com.bing.entity.question.GradeLevelinfo;
import com.bing.entity.question.QuestionInfo;
import com.bing.entity.question.Scoreinfo;
import com.bing.service.LoanProdductsinfoService;
import com.bing.service.QuestionService;
import com.bing.utils.utilstool.MathRandom;
import com.bing.utils.utilstool.MyComparator;
import com.bing.web.BaseController;
import com.bing.web.BaseResponse;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiOperation;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by hurl on 2018-08-19
 */
@RestController
@RequestMapping("/question")
@Component
@Aspect
public class QuestionController extends BaseController{
    @Autowired
    QuestionService questionService;

    @ApiOperation(value = "随机产生问题列表")
    @RequestMapping(value = "getQuestionList")
    public ResponseEntity<BaseResponse> getQuestionList() {
        List<String> newlist = Lists.newArrayList();
        //认知
        List<String> listcontId=questionService.selectQuestionIdByType(QuestionType.COGNITIVE.getCode());
        List<String> listtaseId=questionService.selectQuestionIdByType(QuestionType.TASTE.getCode());
        List<String> listattiId=questionService.selectQuestionIdByType(QuestionType.ATTITUDE.getCode());
        List<String> listId=questionService.selectQuestionIdByType(QuestionType.ACTION.getCode());
        List<String> listrestId=questionService.selectQuestionIdByType(QuestionType.RESTRAINT.getCode());
        //产生的随机数
        int[] bool =  MathRandom.MyRandom(2,listcontId.size());
        int[] bool2 =  MathRandom.MyRandom(2,listtaseId.size());
        int[] bool3 =  MathRandom.MyRandom(1,listattiId.size());
        int[] bool4 =  MathRandom.MyRandom(1,listId.size());
        int[] bool5 =  MathRandom.MyRandom(1,listrestId.size());
        //认知 味道
        newlist.add(listcontId.get(bool[0]));
        newlist.add(listtaseId.get(bool2[0]));

        Random random = new Random();
        int result = random.nextInt(10);
        // 二选一
        if(result<3){
            newlist.add(listattiId.get(bool3[0]));
            newlist.add(listcontId.get(bool[1]));
            newlist.add(listtaseId.get(bool2[1]));
            newlist.add(listId.get(bool4[0]));

        }else if (3<=result&&result<6){
            newlist.add(listId.get(bool4[0]));
            newlist.add(listcontId.get(bool[1]));
            newlist.add(listtaseId.get(bool2[1]));
            newlist.add(listrestId.get(bool5[0]));

        }else{
            newlist.add(listrestId.get(bool5[0]));
            newlist.add(listcontId.get(bool[1]));
            newlist.add(listtaseId.get(bool2[1]));
            newlist.add(listattiId.get(bool3[0]));

        }
        // //所有问题的ID 集合
        List<QuestionInfo> questionInfoList = questionService.selectQuestionlistById(newlist);

        return buildSuccessResponse(questionInfoList);
    }


    @ApiOperation(value = "随机产生问题列表")
    @RequestMapping(value = "")
    public ResponseEntity<BaseResponse> getQuestionList2() {
        List<QuestionInfo> newList1  = new ArrayList<QuestionInfo>();
        List<QuestionInfo> questionInfoList = questionService.getQuestionList();
        Map<String ,List<QuestionInfo>> collectMap =questionInfoList.stream().collect(Collectors.groupingBy(QuestionInfo::getQuestionType));
        for (Map.Entry<String, List<QuestionInfo>> entry : collectMap.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
            if(QuestionType.COGNITIVE.equals(entry.getKey())){
                List<QuestionInfo> infoList = entry.getValue();
                int index = (int) (Math.random() * infoList.size());
                newList1.addAll((Collection<? extends QuestionInfo>) infoList.get(index));
            }
        }
        return buildSuccessResponse(newList1);
    }
    @ApiOperation(value = "根据问题答案确定等级文案")
    @RequestMapping(value = "getCalcResult" ,method = RequestMethod.POST)
    public ResponseEntity<BaseResponse> getClacResults(@RequestBody List<QuestionInfo> questionInfoList){
        List<Scoreinfo> list = Lists.newArrayList();
       for (QuestionInfo questionInfo:questionInfoList){
           Scoreinfo score = questionService.calcScore(questionInfo);
           list.add(score);
       }
       //移除相同类型分数较低的
        for (int i = 0; i < list.size()-1; i++) {
            for (int j = list.size()-1; j > i; j--) {
                if (list.get(j).getQuestionType().equals(list.get(i).getQuestionType())) {
                    if(list.get(j).getScore()>list.get(i).getScore()){
                        list.remove(i);
                    }else{
                        list.remove(j);
                    }

                }
            }
        }
        Collections.sort( list, new MyComparator());

        GradeLevelinfo gradeLevelinfo = new GradeLevelinfo();
        //生成文案
        String typeFirst = list.get(0).getQuestionType();
        String typeSecond = list.get(1).getQuestionType();

        GradeLevelinfo levelinfo = questionService.getLevel(typeFirst);
        GradeLevelinfo level = questionService.getLevel(typeSecond);
        gradeLevelinfo.setLevel(levelinfo.getLevel());
        gradeLevelinfo.setGrade(levelinfo.getGrade()+level.getGrade());
        gradeLevelinfo.setImgurl(levelinfo.getImgurl());

        return buildSuccessResponse(gradeLevelinfo);
    }
}
